package cn.com.webxml;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UI {

    private JFrame frame;
    private JTextField phoneNumberTextField;
    private JTextArea resultArea;

    public UI() {
        initializeUI();
    }

    private void initializeUI() {
        frame = new JFrame("手机号码归属地查询");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 650);
        frame.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        JLabel label = new JLabel("请输入手机号码：");
        phoneNumberTextField = new JTextField(20);
        JButton queryButton = new JButton("查询");

        panel.add(label);
        panel.add(phoneNumberTextField);
        panel.add(queryButton);

        resultArea = new JTextArea();
        resultArea.setEditable(false);

        frame.add(panel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);

        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryPhoneNumber(phoneNumberTextField.getText());
            }
        });
    }

    private void queryPhoneNumber(String phoneNumber) {
        String result = "";
        try {
            MobileCodeWS mobileCodeWS = new MobileCodeWS();
            MobileCodeWSSoap service = mobileCodeWS.getMobileCodeWSSoap();
            String mobileInfo = service.getMobileCodeInfo(phoneNumber, ""); // 示例用户ID，如果是免费用户则可以传入空字符串""
            result = "手机号码信息：" + mobileInfo;
        } catch (Exception ex) {
            ex.printStackTrace();
            result = "查询失败，请稍后再试";
        }

        resultArea.setText(result);
    }

    public void displayUI() {
        frame.setVisible(true);
    }
}